#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &x : a) cin >> x;
  a.insert(a.end(), a.begin(), a.end());
  vector<vector<int>> dp(2 * n, vector<int>(2 * n));
  for (int l = 2 * n - 1; l >= 0; l --) {
    for (int r = l + 2; r <= 2 * n - 1; r ++) {
      for (int k = l + 1; k < r; k ++) {
        dp[l][r] = max(dp[l][r], dp[l][k] + dp[k][r] + a[l] * a[k] * a[r]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i ++) {
    ans = max(ans, dp[i][i + n]);
  }
  cout << ans << endl;
}